import 'package:inmotion_pro/config/const.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/pages/models/vehicle_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import 'vehicle_item_view.dart';

/// 车辆List视图
class SheetVehicleListView extends StatelessWidget {
  // 车辆模型数组
  final List<VehicleModel> vehicleList;
  // 选中事件
  final Function(VehicleModel)? selectOnTap;
  // 搜索事件
  final Function()? searchOnTap;
  // 取消事件
  final Function()? cancelOnTap;
  // 简易版，不带删除、搜索功能、只用于展示车辆列表
  final bool isSimpleVersion;

  const SheetVehicleListView({
    super.key,
    required this.vehicleList,
    this.selectOnTap,
    this.searchOnTap,
    this.cancelOnTap,
    this.isSimpleVersion = false,
  });

  @override
  Widget build(BuildContext context) {
    final itemHeight = 120.w;
    return Stack(
      children: [
        Visibility(
          visible: isSimpleVersion,
          child: Positioned(
            right: CONTENT_MARGIN / 2,
            top: 0,
            child: IconButton(
              iconSize: 24,
              icon: const Icon(Icons.cancel_rounded, color: GrayColor.gray3),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ),
        ),
        Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Padding(
              padding: EdgeInsets.only(
                top: CONTENT_MARGIN * 3 / 2,
                bottom: CONTENT_MARGIN / 2,
              ),
              child: LxText(
                context.s.switch_vehicle,
                fontWeightEnum: LxFontWeightEnum.titleLarge,
                color: Colors.white,
                textAlign: TextAlign.center,
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                  top: CONTENT_MARGIN / 2, bottom: CONTENT_MARGIN),
              child: SizedBox(
                height: vehicleList.length >= 3
                    ? itemHeight * 2.5
                    : itemHeight * vehicleList.length,
                child: SlidableAutoCloseBehavior(
                  child: ListView.builder(
                    addAutomaticKeepAlives: false,
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemCount: vehicleList.length,
                    itemBuilder: (context, index) {
                      VehicleModel item = vehicleList[index];
                      return SizedBox(
                        height: itemHeight,
                        child: Padding(
                          padding: EdgeInsets.only(
                            top: CONTENT_MARGIN / 2,
                            bottom: CONTENT_MARGIN / 2,
                            left: CONTENT_MARGIN,
                            right: CONTENT_MARGIN,
                          ),
                          child: VehicleItemView(
                            model: item,
                            height: itemHeight,
                            selectOnTap: (p0) {
                              if (selectOnTap != null) {
                                selectOnTap!(p0);
                              }
                            },
                          ),
                        ),
                      );
                    },
                  ),
                ),
              ),
            ),
            Visibility(
              visible: !isSimpleVersion,
              child: Padding(
                padding: EdgeInsets.only(bottom: CONTENT_MARGIN),
                child: SizedBox(
                  height: commonButtonHeight,
                  child: Center(
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        SizedBox(
                          width: screenWidth / 3,
                          child: LxButton(
                            borderRadius: BorderRadius.circular(4.w),
                            title: local.cancel,
                            height: 46.w,
                            buttonTypeCustomColor: ButtonTypeColor()
                              ..borderColor = GrayColor.gray2
                              ..textColor = Colors.white.withOpacity(0.8)
                              ..backgroundColor = GrayColor.gray2,
                            onTap: () {
                              if (cancelOnTap != null) {
                                cancelOnTap!();
                              }
                            },
                          ),
                        ),
                        SizedBox(width: CONTENT_MARGIN * 3),
                        SizedBox(
                          width: screenWidth / 3,
                          child: LxButton(
                            borderRadius: BorderRadius.circular(4.w),
                            title: local.search,
                            height: 46.w,
                            onTap: () {
                              if (searchOnTap != null) {
                                searchOnTap!();
                              }
                            },
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }
}
